#include <stdlib.h>
#include <assert.h>
#include "new.h"
 
void *new(const void *_class, ...)
{
    const struct Class *class = _class;
    void *p = calloc(1, class->size);
    assert(p);
    *(const struct Class **)p = class;
    if (class->ctor) 
	{
        va_list ap;
        va_start(ap, _class);
        p = class->ctor(p, &ap);
        va_end(ap);
    }
 
    return p;
}

void *placement_new(const void *_class, void* dst, ...)
{
    const struct Class *class = _class;
    *(const struct Class **)dst = class;
    if (class->ctor) 
	{
        va_list ap;
        va_start(ap, _class);
        dst = class->ctor(dst, &ap);
        va_end(ap);
    }
 
    return dst;
}
 
void delete(void *self)
{
    const struct Class **cp = self;
 
    if (self && *cp && (*cp)->dtor)
	{
        self = (*cp)->dtor(self);
	}
    free(self);
}

void placement_delete(void *self)
{
    const struct Class **cp = self;
 
    if (self && *cp && (*cp)->dtor)
	{
        self = (*cp)->dtor(self);
	} 
}